/*
 * Created by JFormDesigner on Fri Jun 04 10:50:45 CST 2021
 */

package com.J1ay.carsys.gui.Emp;


import com.J1ay.carsys.dto.LoginInput;

import com.J1ay.carsys.dto.LoginOutput;
import com.J1ay.carsys.gui.tishi.Success;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.service.Impl.EmpServiceImpl;

import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.*;

/**
 * @author Brainrain
 */
public class Login extends JFrame {
    public Login() {
        initComponents();
    }
    public void start() {
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


    private void loginActionPerformed(final ActionEvent e) {
        // TODO add your code here
        String username = this.username.getText();
        String reg = "^[a-z0-9A-Z]+$";
        try {
            if (!username.matches(reg)){
                new Warning(this, "用户名格式有异！").start();
            }
            else{
                LoginInput loginInput = new LoginInput();
                EmpService empService = new EmpServiceImpl();

                loginInput.setUsername(this.username.getText());
                loginInput.setPassword(String.valueOf(this.password.getPassword()));
                System.out.println(loginInput);
                final Result Result = empService.login(loginInput);
                System.out.println(Result);
                if(200 == Result.getStatus()) {
                    System.out.println(Result.getData());
                    new Success(this,"登录成功！").start();

                    Timer timer1 = new Timer();
                    timer1.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            new Main((LoginOutput)Result.getData()).start();
                            // 关闭窗口
                            JButton jButton = (JButton) e.getSource();
                            Container container = jButton.getParent();
                            while (!(container instanceof JFrame)) {
                                container = container.getParent();
                            }
                            ((JFrame)container).dispose();
                        }
                    },100);
                }
                else {
                    new Warning(this,Result.getMsg()).start();
                    this.password.setText("");
                }
            }
        } catch (Exception e1){
            e1.printStackTrace();
            new Warning(this, "用户名格式有异！").start();
        }
    }


    private void passwordKeyPressed(final KeyEvent e) {
        // TODO add your code here
        if(e.getKeyChar() == KeyEvent.VK_ENTER ) {
            String username = this.username.getText();
            String reg = "^[a-z0-9A-Z]+$";
            try {
                if (!username.matches(reg)){
                    new Warning(this, "用户名格式有异！").start();
                }
                else{
                    LoginInput loginInput = new LoginInput();
                    EmpService empService = new EmpServiceImpl();
                    loginInput.setUsername(this.username.getText());
                    loginInput.setPassword(String.valueOf(this.password.getPassword()));
                    System.out.println(loginInput);
                    Result Result = empService.login(loginInput);
                    System.out.println(Result);
                    if(200 == Result.getStatus()) {
                        System.out.println(Result.getData());
                        new Success(this,"登录成功！").start();
                        new Main((LoginOutput)Result.getData()).start();
                        // 关闭窗口
                        JPasswordField jPasswordField = (JPasswordField) e.getSource();
                        Container container = jPasswordField.getParent();
                        while (!(container instanceof JFrame)) {
                            container = container.getParent();
                        }
                        ((JFrame)container).dispose();
                    }
                    else {
                        new Warning(this,Result.getMsg()).start();
                        this.password.setText("");
                    }
                }
            } catch (Exception e1){
                e1.printStackTrace();
                new Warning(this, "用户名格式有异！").start();
            }
        }
    }

    private void loginMouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void loginKeyPressed(KeyEvent e) {
        // TODO add your code here
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        labelUser = new JLabel();
        labelPwd = new JLabel();
        login = new JButton();
        username = new JTextField();
        label3 = new JLabel();
        password = new JPasswordField();

        //======== this ========
        setTitle("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7cfb\u7edf");
        setBackground(new Color(252, 252, 252));
        setResizable(false);
        Container contentPane = getContentPane();

        //---- labelUser ----
        labelUser.setText("\u7528\u6237\u540d\uff1a");

        //---- labelPwd ----
        labelPwd.setText("\u5bc6\u7801\uff1a");

        //---- login ----
        login.setText("\u767b\u5f55");
        login.setBackground(Color.white);
        login.setForeground(Color.darkGray);
        login.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
        login.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                loginMouseClicked(e);
            }
        });
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginActionPerformed(e);
            }
        });
        login.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                loginKeyPressed(e);
            }
        });

        //---- username ----
        username.setBackground(Color.white);

        //---- label3 ----
        label3.setText("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7ba1\u7406\u7cfb\u7edf");

        //---- password ----
        password.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                passwordKeyPressed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap(97, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(labelPwd, GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
                                .addComponent(labelUser, GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(username, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
                                .addComponent(password))
                            .addGap(82, 82, 82))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addComponent(label3)
                            .addGap(142, 142, 142))))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addContainerGap(186, Short.MAX_VALUE)
                    .addComponent(login)
                    .addGap(157, 157, 157))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addComponent(label3, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(labelUser, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(username))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(labelPwd, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(password, GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                    .addComponent(login)
                    .addGap(38, 38, 38))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel labelUser;
    private JLabel labelPwd;
    private JButton login;
    private JTextField username;
    private JLabel label3;
    private JPasswordField password;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
